<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { ElCard, ElRow, ElCol, ElStatistic, ElLoading } from 'element-plus'
import { User, Money, Present, VideoPlay } from '@element-plus/icons-vue'
import request from '@/utils/request'

interface DashboardStats {
  users: {
    total: number
    active: number
    partners: number
    todayNew: number
  }
  recharges: {
    total: number
    approved: number
    pending: number
    today: number
    totalAmount: number
    approvedAmount: number
  }
  withdrawals: {
    total: number
    approved: number
    pending: number
    totalAmount: number
    approvedAmount: number
  }
  gifts: {
    totalSends: number
    todaySends: number
    totalPoints: string
    todayPoints: string
    totalChatGifts: number
    todayChatGifts: number
    totalChatGiftPoints: string
    todayChatGiftPoints: string
  }
  content: {
    totalChannels: number
    activeChannels: number
    totalLiveRooms: number
    activeLiveRooms: number
    totalChatRooms: number
    activeChatRooms: number
  }
}

const loading = ref(true)
const stats = ref<DashboardStats | null>(null)

const fetchDashboardStats = async () => {
  try {
    loading.value = true
    const response = await request.get('/api/admin/dashboard')
    stats.value = response.data
  } catch (error) {
    console.error('대시보드 데이터 가져오기 실패:', error)
  } finally {
    loading.value = false
  }
}

onMounted(() => {
  fetchDashboardStats()
})

// 큰 숫자 포맷
const formatNumber = (num: number | string) => {
  const n = typeof num === 'string' ? parseInt(num) : num
  if (n >= 10000) {
    return (n / 10000).toFixed(1) + '만'
  }
  return n.toLocaleString()
}

// 금액 포맷
const formatAmount = (amount: number) => {
  return '¥' + amount.toLocaleString()
}
</script>

<template>
  <div class="dashboard-container">
    <div class="page-header">
      <h1 class="page-title">대시보드</h1>
      <p class="page-subtitle">시스템 운영 데이터 개요</p>
    </div>

    <div v-loading="loading" class="dashboard-content">
      <template v-if="stats">
        <!-- 사용자 통계 -->
        <div class="stats-section">
          <h3 class="section-title">
            <el-icon><User /></el-icon>
            사용자 통계
          </h3>
          <el-row :gutter="20">
            <el-col :span="6">
              <el-card class="stat-card">
                <el-statistic
                  title="총 등록 회원 수"
                  :value="stats.users.total"
                  :formatter="formatNumber"
                />
              </el-card>
            </el-col>
            <el-col :span="6">
              <el-card class="stat-card">
                <el-statistic
                  title="활성 사용자"
                  :value="stats.users.active"
                  :formatter="formatNumber"
                />
              </el-card>
            </el-col>
            <el-col :span="6">
              <el-card class="stat-card">
                <el-statistic
                  title="파트너"
                  :value="stats.users.partners"
                  :formatter="formatNumber"
                />
              </el-card>
            </el-col>
            <el-col :span="6">
              <el-card class="stat-card">
                <el-statistic
                  title="오늘 신규"
                  :value="stats.users.todayNew"
                  :formatter="formatNumber"
                />
              </el-card>
            </el-col>
          </el-row>
        </div>

        <!-- 충전 통계 -->
        <div class="stats-section">
          <h3 class="section-title">
            <el-icon><Money /></el-icon>
            충전 통계
          </h3>
          <el-row :gutter="20">
            <el-col :span="6">
              <el-card class="stat-card">
                <el-statistic
                  title="충전 주문 총수"
                  :value="stats.recharges.total"
                  :formatter="formatNumber"
                />
              </el-card>
            </el-col>
            <el-col :span="6">
              <el-card class="stat-card">
                <el-statistic
                  title="승인된 주문"
                  :value="stats.recharges.approved"
                  :formatter="formatNumber"
                />
              </el-card>
            </el-col>
            <el-col :span="6">
              <el-card class="stat-card">
                <el-statistic
                  title="대기 중인 주문"
                  :value="stats.recharges.pending"
                  :formatter="formatNumber"
                />
              </el-card>
            </el-col>
            <el-col :span="6">
              <el-card class="stat-card">
                <el-statistic
                  title="오늘 충전 주문"
                  :value="stats.recharges.today"
                  :formatter="formatNumber"
                />
              </el-card>
            </el-col>
          </el-row>
          <el-row :gutter="20" style="margin-top: 20px;">
            <el-col :span="12">
              <el-card class="stat-card">
                <el-statistic
                  title="충전 총 금액"
                  :value="stats.recharges.totalAmount"
                  :formatter="formatAmount"
                />
              </el-card>
            </el-col>
            <el-col :span="12">
              <el-card class="stat-card">
                <el-statistic
                  title="승인된 금액"
                  :value="stats.recharges.approvedAmount"
                  :formatter="formatAmount"
                />
              </el-card>
            </el-col>
          </el-row>
        </div>

        <!-- 출금 통계 -->
        <div class="stats-section">
          <h3 class="section-title">
            <el-icon><Money /></el-icon>
            출금 통계
          </h3>
          <el-row :gutter="20">
            <el-col :span="6">
              <el-card class="stat-card">
                <el-statistic
                  title="출금 신청 총수"
                  :value="stats.withdrawals.total"
                  :formatter="formatNumber"
                />
              </el-card>
            </el-col>
            <el-col :span="6">
              <el-card class="stat-card">
                <el-statistic
                  title="승인된 신청"
                  :value="stats.withdrawals.approved"
                  :formatter="formatNumber"
                />
              </el-card>
            </el-col>
            <el-col :span="6">
              <el-card class="stat-card">
                <el-statistic
                  title="대기 중인 신청"
                  :value="stats.withdrawals.pending"
                  :formatter="formatNumber"
                />
              </el-card>
            </el-col>
            <el-col :span="6">
              <el-card class="stat-card">
                <el-statistic
                  title="출금 총 금액"
                  :value="stats.withdrawals.totalAmount"
                  :formatter="formatAmount"
                />
              </el-card>
            </el-col>
          </el-row>
        </div>

        <!-- 선물 통계 -->
        <div class="stats-section">
          <h3 class="section-title">
            <el-icon><Present /></el-icon>
            선물 통계
          </h3>
          <el-row :gutter="20">
            <el-col :span="6">
              <el-card class="stat-card">
                <el-statistic
                  title="라이브 선물 총수"
                  :value="stats.gifts.totalSends"
                  :formatter="formatNumber"
                />
              </el-card>
            </el-col>
            <el-col :span="6">
              <el-card class="stat-card">
                <el-statistic
                  title="오늘 라이브 선물"
                  :value="stats.gifts.todaySends"
                  :formatter="formatNumber"
                />
              </el-card>
            </el-col>
            <el-col :span="6">
              <el-card class="stat-card">
                <el-statistic
                  title="채팅 선물 총수"
                  :value="stats.gifts.totalChatGifts"
                  :formatter="formatNumber"
                />
              </el-card>
            </el-col>
            <el-col :span="6">
              <el-card class="stat-card">
                <el-statistic
                  title="오늘 채팅 선물"
                  :value="stats.gifts.todayChatGifts"
                  :formatter="formatNumber"
                />
              </el-card>
            </el-col>
          </el-row>
          <el-row :gutter="20" style="margin-top: 20px;">
            <el-col :span="12">
              <el-card class="stat-card">
                <el-statistic
                  title="라이브 선물 총 포인트"
                  :value="Number(stats.gifts.totalPoints)"
                  :formatter="formatNumber"
                />
              </el-card>
            </el-col>
            <el-col :span="12">
              <el-card class="stat-card">
                <el-statistic
                  title="채팅 선물 총 포인트"
                  :value="Number(stats.gifts.totalChatGiftPoints)"
                  :formatter="formatNumber"
                />
              </el-card>
            </el-col>
          </el-row>
        </div>

        <!-- 콘텐츠 통계 -->
        <div class="stats-section">
          <h3 class="section-title">
            <el-icon><VideoPlay /></el-icon>
            콘텐츠 통계
          </h3>
          <el-row :gutter="20">
            <el-col :span="6">
              <el-card class="stat-card">
                <el-statistic
                  title="채널 총수"
                  :value="stats.content.totalChannels"
                  :formatter="formatNumber"
                />
              </el-card>
            </el-col>
            <el-col :span="6">
              <el-card class="stat-card">
                <el-statistic
                  title="활성 채널"
                  :value="stats.content.activeChannels"
                  :formatter="formatNumber"
                />
              </el-card>
            </el-col>
            <el-col :span="6">
              <el-card class="stat-card">
                <el-statistic
                  title="라이브 방 총수"
                  :value="stats.content.totalLiveRooms"
                  :formatter="formatNumber"
                />
              </el-card>
            </el-col>
            <el-col :span="6">
              <el-card class="stat-card">
                <el-statistic
                  title="활성 라이브 방"
                  :value="stats.content.activeLiveRooms"
                  :formatter="formatNumber"
                />
              </el-card>
            </el-col>
          </el-row>
          <el-row :gutter="20" style="margin-top: 20px;">
            <el-col :span="12">
              <el-card class="stat-card">
                <el-statistic
                  title="총 채팅방 수"
                  :value="stats.content.totalChatRooms"
                  :formatter="formatNumber"
                />
              </el-card>
            </el-col>
            <el-col :span="12">
              <el-card class="stat-card">
                <el-statistic
                  title="활성 채팅방"
                  :value="stats.content.activeChatRooms"
                  :formatter="formatNumber"
                />
              </el-card>
            </el-col>
          </el-row>
        </div>
      </template>
    </div>
  </div>
</template>

<style scoped>
.dashboard-container {
  height: 100%;
  display: flex;
  flex-direction: column;
}

.page-header {
  margin-bottom: 24px;
}

.page-title {
  font-size: 24px;
  font-weight: 600;
  color: var(--el-text-color-primary);
  margin: 0 0 8px 0;
}

.page-subtitle {
  font-size: 14px;
  color: var(--el-text-color-regular);
  margin: 0;
}

.dashboard-content {
  flex: 1;
  min-height: 400px;
}

.stats-section {
  margin-bottom: 32px;
}

.section-title {
  font-size: 18px;
  font-weight: 600;
  color: var(--el-text-color-primary);
  margin: 0 0 16px 0;
  display: flex;
  align-items: center;
  gap: 8px;
}

.stat-card {
  height: 120px;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.3s ease;
}

.stat-card:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}

.stat-card :deep(.el-statistic__head) {
  font-size: 14px;
  color: var(--el-text-color-regular);
  margin-bottom: 8px;
}

.stat-card :deep(.el-statistic__content) {
  font-size: 24px;
  font-weight: 600;
  color: var(--el-color-primary);
}
</style>
